<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入批次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库时间" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="领用时间" prop="dateRange2">
        <el-date-picker
          v-model="dateRange2"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
        ></el-date-picker>
      </el-form-item>
      
      
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:inventory:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form
          style="margin-left:50px"
            label-position="left"
            inline
            class="demo-table-expand"
            v-for="item in props.row.consumeGoodsOut"
          >
            <el-row>
              <el-form-item style="width: 200px;margin-bottom: 0;" label="领用日期">
                <span>{{ item.outDate }}</span>
              </el-form-item>
              <el-form-item style="width: 200px;margin-bottom: 0;" label="领用数量">
                <span>{{ item.num }}</span>
              </el-form-item>
              <el-form-item style="width: 200px;margin-bottom: 0;" label="剩余库存">
                <span>{{ item.currentNum - item.num }}</span>
              </el-form-item>
            </el-row>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        min-width="150"
        label="名称"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="primary">{{
            scope.row.name
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="入库批次"
        min-width="150"
        prop="code"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        min-width="120"
        label="部门"
        prop="dept"
      >
      </el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="技术指标"
        min-width="150"
        prop="tecTarget"
      /> -->
       <el-table-column
        show-overflow-tooltip
        min-width="150"
        label="技术要求"
        prop="directory.techInfo"
      />
      <el-table-column
        show-overflow-tooltip
        min-width="150"
        label="主要用途"
        prop="directory.useInfo"
      />
      <el-table-column
        show-overflow-tooltip
        label="规格"
        width="150"
        prop="spec"
      />
      
      <el-table-column show-overflow-tooltip label="数量" width="150" prop="num"> <template slot-scope="scope">
          <span>{{ Number(scope.row.num) - Number(scope.row.totalOutNum).toFixed(2) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        min-width="150"
        label="有效期"
        prop="limitDate"
      />
      <el-table-column
        show-overflow-tooltip
        min-width="150"
        label="入库日期"
        prop="joinDate"
      />

      <el-table-column
        show-overflow-tooltip
        min-width="150"
        label="保管人"
        prop="managerUser"
      />
      <el-table-column
        show-overflow-tooltip
        min-width="150"
        label="创建人"
        prop="createByName"
      />
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['company:news:info']"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['company:news:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:news:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listconsume_Join,
  delconsume_Join,
  effectgoodsDirectory,
} from "@/api/quality/goods/consumeIn.js";
export default {
  name: "consumeIn",
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        code:undefined,
        spec:undefined,
        chemicals:0
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
      dateRange2:[]
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.joinDateStart) {
        this.dateRange.push(this.queryParams.joinDateStart, this.queryParams.joinDateEnd)
      }
      if (this.queryParams.outDateStart) {
        this.dateRange2.push(this.queryParams.outDateStart, this.queryParams.outDateEnd)
      }
      
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    handleStatusChange(row) {
      let text = row.effectStatus === "1" ? "启用" : "禁用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.name + '"目录吗？')
        .then(function () {
          return effectgoodsDirectory(row.consumeGoodsJoinId, row.effectStatus);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.effectStatus = row.effectStatus === "0" ? "1" : "0";
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.consumeGoodsJoinId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.consumeGoodsJoinId || this.ids;
      this.$router.push("./addConsumeIn?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push(
        "./addConsumeIn?isInfo=true&id=" + row.consumeGoodsJoinId
      );
    },
    handleExport() {
      this.queryParams.ids = this.ids.toString()
      this.download(
        "system/consume_Join/stockExport",
        {
          ...this.queryParams,
        },
        `消耗品库存表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.consumeGoodsJoinId || this.ids;

      this.$modal
        .confirm('是否确认删除ID为"' + configId + '"的数据项？')
        .then(function () {
          return delconsume_Join(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push("./addConsumeIn");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listconsume_Join({
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "joinDateStart",
          "joinDateEnd"
        ),
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange2,
          "outDateStart",
          "outDateEnd"
        ),
      }).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.dateRange2 =[]
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-top: 20px !important;
}
